package com.mycompany.myapp.web.rest.controller;

import com.mycompany.myapp.service.CartItemService;
import com.mycompany.myapp.service.CartService;
import com.mycompany.myapp.service.dto.CartItemDTO;
import com.mycompany.myapp.web.rest.utils.ResultObj;
import com.mycompany.myapp.web.rest.vm.CartItemVM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;

@Api(tags = {"购物车管理"})
@RestController
@RequestMapping("/api")
public class CartController {

    private final CartService cartService;

    private final CartItemService cartItemService;

    public CartController(CartService cartService, CartItemService cartItemService) {
        this.cartService = cartService;
        this.cartItemService = cartItemService;
    }

    @ApiOperation("获取购物车 RequestParam")
    @PostMapping("/cart/userId")
    public ResultObj getCartByUserId(Long userId) throws URISyntaxException {
        return ResultObj.back(200,cartService.getCartByUserId(userId));
    }

    @ApiOperation("添加购物车项 RequestBody")
    @PostMapping("/cartItem/add")
    public ResultObj addCartItem(@RequestBody CartItemVM cartItemVM) throws URISyntaxException{
        return cartItemService.addCartItem(cartItemVM);
    }

    @ApiOperation("删除购物车项 RequestParam")
    @DeleteMapping("/cartItem/cartItemId")
    public ResultObj removeCartItem(Long cartItemId) throws URISyntaxException{
        cartItemService.delete(cartItemId);
        return ResultObj.back(200,"删除成功",null);
    }


}
